/**
 * @Author yinyunqi
 * @date 2017年12月28日
 * @Content 
 */
package com.yongqi.controller.console;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yongqi.model.Family;
import com.yongqi.model.console.Bill;
import com.yongqi.pub.BaiduMapTranslate;
import com.yongqi.pub.DateFormat;
import com.yongqi.service.MemberService;
import com.yongqi.service.console.PayService;

@Controller
@RequestMapping("/pay")
public class PayController {
	@Autowired
	PayService payService;
	@Autowired
	MemberService memberService;

	/**
	 * 跳转至支出主页面 根据本家庭账户获得该家庭账户下拥有的成员账户
	 * 
	 * @return
	 */
	@RequestMapping("/index")
	public String payIndex(Model model) {
		String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		List<String> memberNametList = memberService.selectMemberByFamily(familyAccount);
		model.addAttribute("nameList", memberNametList);
		return "pay/PayIndex";
	}

	/**
	 * 添加支出记录
	 * 
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	@ResponseBody
	@RequestMapping("/addItem")
	public String addItem(HttpServletRequest request) throws SQLException {
		String address = null;
		//前台获取经纬度并工具类解析
		String lng = request.getParameter("lng");//经度
		String lat = request.getParameter("lat");//纬度
		System.out.println(lat+","+lng);
		address = BaiduMapTranslate.addressTranslate(lat, lng);
		
		Bill pay = new Bill();
		String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		pay.setId(UUID.randomUUID().toString());
		pay.setFamilyAccount(familyAccount);
		pay.setMemberAccount(familyAccount);
		pay.setDate(request.getParameter("payDate"));
		pay.setItem(request.getParameter("payItem"));
		pay.setTip(request.getParameter("payTip"));
		pay.setType(request.getParameter("payType"));
		pay.setTypeDetail(request.getParameter("payTypeDetail"));
		pay.setMember(request.getParameter("payMember"));
		pay.setMoney(request.getParameter("payMoney"));
		pay.setLng(request.getParameter("lng"));
		pay.setLat(request.getParameter("lat"));
		pay.setBillType("pay");
		pay.setCreated_time(DateFormat.dateFormate());
		pay.setAddress(request.getParameter("address"));
		payService.payAdd(pay);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", "success");
		return jsonObject.toJSONString();
	}

	/**
	 * 收入分页查询
	 * 
	 * @param request
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/query")
	public String queryItems(HttpServletRequest request, @RequestParam int pageNumber, int pageSize) {
		Map<String, String> map = new HashMap<String, String>();
		String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		map.put("billType", "pay");
		map.put("familyAccount", familyAccount);
		int total = payService.queryPageItems(map).size();
		PageHelper.startPage(pageNumber, pageSize);
		List<Bill> incomeList = payService.queryPageItems(map);
		JSONObject object = new JSONObject();
		object.put("rows", incomeList);
		object.put("total", total);
		return object.toJSONString();
	}

	/**
	 * 删除收入记录
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public String deleteItems(HttpServletRequest request) {
		String[] ids = request.getParameterValues("ids");
		for (int i = 0; i < ids.length; i++) {
			payService.deletePayItems(ids[i]);
		}
		JSONObject object = new JSONObject();
		object.put("result", "success");
		return object.toJSONString();
	}

	/**
	 * 修改收入记录
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateItem")
	public String updateItem(HttpServletRequest request) {
		Bill pay = new Bill();
		String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		pay.setId(request.getParameter("id"));
		pay.setFamilyAccount(familyAccount);
		pay.setMemberAccount(familyAccount);
		pay.setDate(request.getParameter("payDate"));
		pay.setItem(request.getParameter("payItem"));
		pay.setTip(request.getParameter("payTip"));
		pay.setType(request.getParameter("payType"));
		pay.setTypeDetail(request.getParameter("payTypeDetail"));
		pay.setMember(request.getParameter("payMember"));
		pay.setMoney(request.getParameter("payMoney"));
		pay.setBillType("pay");
		payService.payUpdate(pay);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", "success");
		return jsonObject.toJSONString();
	}
}
